<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
 
class ModAnnibalQuote
{
 
    function getQuote( $params )
	{
		$annibalquotes_apikey = $params->get('annibalquotes_apikey', 'TEST KEY');
		$annibalquotes_showref = $params->get('annibalquotes_showref');
		$annibalquotes_imagealign = $params->get('annibalquotes_imagealign');
		$annibalquotes_lang = $params->get('annibalquotes_lang', 'it');
		$moduleclass_sfx = $params->get('moduleclass_sfx');
		$annibalquotes_ver = "2.0.0";
	
		$cachefile = "tmp/annibalquote" . $annibalquotes_lang . ".txt";
		$cache = @file_get_contents($cachefile);
	
		if($cache)
		{
			$cachef = unserialize($cache);
			if($cachef['date'] == date("md"))
			{
				return $cachef['result'];
			}
		}
		$xml = file_get_contents("http://rcj.org/" . $annibalquotes_lang . "/component/aquote/quote/daily?format=raw");
		
		$xml_parser =  xml_parser_create();
		xml_parse_into_struct($xml_parser, $xml, $vals, $index);
		xml_parser_free($xml_parser);
		
		$result = array('quote' => '', 'source' => '');
		foreach($vals as $xml_elt)
		{
			if($xml_elt['tag'] == 'TEXT')
			{
				$result['quote'] = $xml_elt['value'];
			}
			elseif($xml_elt['tag'] == 'SOURCE')
			{
				$result['source'] = $xml_elt['value'];
			}
		}
		
		$cachef = array('date' => date("md"), 'result' => $result);
		$cache = serialize($cachef);
		file_put_contents($cachefile, $cache);
		
		return $result;
		
	}
} //end SimplestForumLatestPostsHelper
